package org.linlinjava.litemall.admin.config;

import org.linlinjava.litemall.db.util.SystemUtil;
import org.linlinjava.litemall.db.util.UploadConstant;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * @author wj
 * @date 2021/7/16 16:21
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                //.allowedHeaders("Origin", "X-Requested-With", "Content-Type", "Accept", "X-Token", "content-type")
                .allowedHeaders("*")  //服务器允许的请求头
                .allowedMethods("POST", "PUT", "GET", "OPTIONS", "DELETE")  //服务器允许的请求方法
                .allowCredentials(true)  //允许带 cookie 的跨域请求
                .allowedOrigins("*")  //服务端允许哪些域请求资源
                .maxAge(3600);   //预检请求的缓存时间
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //文件的虚拟地址和本地磁盘地址映射
        if(SystemUtil.isWindows()){
            registry.addResourceHandler("/upload/**").addResourceLocations("file:"+ UploadConstant.WINDOS_BASE_UPLOAD_URL);
        }
        else if(SystemUtil.isLinux()){
            registry.addResourceHandler("/upload/**").addResourceLocations("file:"+UploadConstant.LINUX_BASE_UPLOAD_URL);
        }
        else if(SystemUtil.isMacOSX()||SystemUtil.isMacOS()){
            registry.addResourceHandler("/upload/**").addResourceLocations("file:"+UploadConstant.MAC_BASE_UPLOAD_URL);
        }

    }

}
